<?php 
include ('conn.php');

// 参数为一个sql语句，判断是否存在一个记录
function has_one ($sql) {
    global $db;
    $result = mysql_query ($sql, $db);
    if (! $result)
        die (err_msg ('查询数据库失败'));
    $row = mysql_fetch_array ($result);
    if (! $row)
        return 0;
    else
        return $row;
}

// 判断是否为好友，参数为两个用户的id
function has_friend($f1,$f2) {
    global $db;
    $sql = "select * from friends where owner = $f1 and friend = $f2 limit 1";
    return has_one ($sql);
}

// 是否丰在该帐号，参数为用户id
function has_user ($id) {
    global $db;
    $sql = "select * from user where uid = $id limit 1";
    return has_one ($sql);
}

// 是否存在该帐号，参数为用户名
function has_username ($name) {
    global $db;
    $sql = "select * from user where name = '$name' limit 1";
    return has_one ($sql);
    //return (mysql_fetch_array(mysql_query($sql, $db)));
}

?>
